import { menuEmits } from "element-plus";
import { defineStore } from "pinia";
import {store} from '../index'

interface AdminStore {
  menus: MenusType[];
}

export const useAdminStore = defineStore("admin", {
  state(): AdminStore {
    return {
      menus: [],
    };
  },
  actions: {
    updateMenus(menus: MenusType[]) {
      this.menus = menus;
    },
  },
  getters: {
    genNewMenus(state) {
      // extend约束  in：映射（循环） for(let key in obj)
      const newMenus: Recordable<MenusType> = {};
      state.menus.forEach((menu) => {
        if (menu.parentId === 0) {
          newMenus[menu.id] = {
            ...menu,
            children: newMenus[menu.parentId]?.children ?? [],
          };
        } else {
          // 子菜单所对应的父id
          newMenus[menu.parentId] ??= {} as MenusType;
          newMenus[menu.parentId].children ??= [];
          newMenus[menu.parentId].children.push(menu);
        }
      });
      return newMenus;
    },
  },
});

export const useAdminStoreWithOut=()=>useAdminStore(store)
